package hibu.LeiJonGun.activities;

import hibu.LeiJonGun.seniorservice.Control;
import hibu.LeiJonGun.seniorservice.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

/* 
 * Written by: Christian L. Gunvaldsen
 * This activity is the main screen and the first activity
 * that the user will see when they log in.
 * 
 * The main screen can take the user to 3 places:
 *  - Search for users
 *  - The users network
 *  - Assignments
 */
public class ActMainScreen extends Activity {

	private Control control;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_main_screen);
		
		//This "import" the control object so that the activity can use the control to handle database tasks:
		control = ((Control)this.getApplicationContext()); 
		control.setContext(ActMainScreen.this);
		
	//Get views:
		//assignment
		final LinearLayout layoutAssignment= (LinearLayout) findViewById(R.id.layoutAssignment);
		final ImageButton imgAssignment = (ImageButton) findViewById(R.id.btnAssignment);
		final TextView lblAssignment = (TextView) findViewById(R.id.lblAssignment);
		//add contact
		final LinearLayout layoutAddContact = (LinearLayout) findViewById(R.id.layoutAddContact);
		final ImageButton imgAddContact = (ImageButton) findViewById(R.id.btnUserSearch);
		final TextView lblAddContact = (TextView) findViewById(R.id.lblAddContact);
		//network
		final LinearLayout layoutNetwork = (LinearLayout) findViewById(R.id.layoutNetwork);
		final ImageButton imgNetwork = (ImageButton) findViewById(R.id.btnNetwork);
		final TextView lblNetwork = (TextView) findViewById(R.id.lblNetwork);
		
	//Create listener. This will be a default listener that is used by all the elements
		final View.OnTouchListener onPressedMainMenu = new View.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				// When user push down
				// Go to pressed image/textcolor
				if (event.getAction()==MotionEvent.ACTION_DOWN) {; 
					// Check witch layout,image or text that was pressed
					if (v.getTag().toString().equals("assignment")) {
						lblAssignment.setTextColor(getResources().getColor(R.color.mainScreenPressed));
						imgAssignment.setBackgroundResource(R.drawable.assignment_pressed);
					} else if (v.getTag().toString().equals("addContact")) {
						imgAddContact.setBackgroundResource(R.drawable.add_contact_pressed);
						lblAddContact.setTextColor(getResources().getColor(R.color.mainScreenPressed));
					} else if (v.getTag().toString().equals("network")) {
						imgNetwork.setBackgroundResource(R.drawable.network_pressed);
						lblNetwork.setTextColor(getResources().getColor(R.color.mainScreenPressed));
					}
					
				// When user let go (release view)
				// Go to default image/textcolor 
				} else if(event.getAction()==MotionEvent.ACTION_UP){
					// Check witch layout,image or text that was pressed
					if (v.getTag().toString().equals("assignment")) {
						lblAssignment.setTextColor(getResources().getColor(R.color.lblText));
						imgAssignment.setBackgroundResource(R.drawable.assignment);
						Intent intent = new Intent(ActMainScreen.this,ActViewAssignment.class);
						startActivity(intent);
					} else if (v.getTag().toString().equals("addContact")) {
						imgAddContact.setBackgroundResource(R.drawable.add_contact);
						lblAddContact.setTextColor(getResources().getColor(R.color.lblText));
						Intent intent = new Intent(ActMainScreen.this,ActFindUser.class);
						startActivity(intent);
					} else if (v.getTag().toString().equals("network")) {
						imgNetwork.setBackgroundResource(R.drawable.network);
						lblNetwork.setTextColor(getResources().getColor(R.color.lblText));
						Intent intent = new Intent(ActMainScreen.this,ActNetwork.class);
						startActivity(intent);
					}
				}
				return true;
			}
		};
		
	//Add listeners:
		//assignment
		layoutAssignment.setOnTouchListener(onPressedMainMenu);
		imgAssignment.setOnTouchListener(onPressedMainMenu);
		lblAssignment.setOnTouchListener(onPressedMainMenu);
		//add contact
		layoutAddContact.setOnTouchListener(onPressedMainMenu);
		imgAddContact.setOnTouchListener(onPressedMainMenu);
		lblAddContact.setOnTouchListener(onPressedMainMenu);
		//network
		layoutNetwork.setOnTouchListener(onPressedMainMenu);
		imgNetwork.setOnTouchListener(onPressedMainMenu);
		lblNetwork.setOnTouchListener(onPressedMainMenu);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.act_main_screen, menu);
		return true;
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle menu click
        switch (item.getItemId()) {
        case R.id.log_out:
        	control.logOut();
        	Intent intent = new Intent(ActMainScreen.this,ActLogIn.class);
			startActivity(intent);
        	this.finish();
            return true;
        case R.id.act_main_screen_help:
        	String url = "http://www.lillesto.net/hjelpeside/index.html";
        	Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        	startActivity(i);
        	return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	    	moveTaskToBack(true);
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}

}
